package cn.itcast.bos.consumer;

import cn.itcast.bos.utils.SmsDemoUtils;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import org.springframework.stereotype.Component;

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.List;

/**
 * Created by Think on 2017/9/22.
 */
@Component("queueConsumer1")
public class RegistConsumer implements MessageListener {

    public void onMessage(Message message) {
        MapMessage mapMessage = (MapMessage) message;
        try {
            String telephone = mapMessage.getString("telephone");
            String code = mapMessage.getString("code");
            SendSmsResponse sendSmsCode = SmsDemoUtils.sendSms(telephone, code);
            if(sendSmsCode.getCode().equals("OK")){
                QuerySendDetailsResponse querySendDetails = SmsDemoUtils.querySendDetails(sendSmsCode.getBizId());
                // 发送成功
                System.out.println("发送短信成功,手机号："
                        + telephone + "，验证码："
                        + code );
                List<QuerySendDetailsResponse.SmsSendDetailDTO> smsSendDetailDTOs = querySendDetails.getSmsSendDetailDTOs();
                for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO :smsSendDetailDTOs){
                    System.out.println("短信内容："+smsSendDetailDTO.getContent());
                }
            }
        } catch (Exception e) {
            System.out.println("发送短信失败!");
            e.printStackTrace();
        }
    }
}
